<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\controller\Backend;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $today_end_time = strtotime(date('Y-m-d').'23:59:59');
        $today_start_time = strtotime(date('Y-m-d').'00:00:00');
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $userTotal = User::count();
        $userAdd = User::where('created_time','between',[$today_start_time,$today_end_time])->count();
        $user_login = User::where('last_login_time','>',$today_start_time)->count();
        $user_loss = User::where('last_login_time','<',time()-30*86400)->count();
        $user_brisk = count(Db::query("select u_id,count(u_id) AS count from panda_prop_log where time> {$today_start_time} group by u_id HAVING count > 10 "));
        $rawal = Db::name('money_log')->where(['status'=>1])->sum('money');
        $wait_rawal = Db::name('money_log')->where(['status'=>0])->sum('money');
        $money = User::sum('money');
        $pandaMoney = User::sum('panda_money');

        $rmb = User::sum('rmb');
        $this->view->assign([
            'totaluser'        => $userTotal,
            'totalviews'       => $money,
            'totalorder'       => $pandaMoney,
            'totaladd'         => $userAdd,
            'total_loss'       => $user_loss,
            'totallogin'       => $user_login,
            'totalbrisk'       => $user_brisk,
            'total_rawal'      => $rawal,
            'total_wait_rawal' => $wait_rawal,
            'totalorderamount' => $rmb,
            'todayuserlogin'   => 321,
            'todayusersignup'  => 430,
            'todayorder'       => 2324,
            'unsettleorder'    => 132,
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
